package cn.shop.admin.controller;

import cn.shop.admin.entity.mainSiteAnnouncementModule.MainSiteAnnouncement;
import cn.shop.admin.entity.mainSiteAnnouncementModule.MainSiteGames;
import cn.shop.admin.service.BackSettingService;
import cn.shop.admin.service.MainSiteAnnouncementService;
import cn.shop.admin.service.MainSiteGamesService;
import cn.shop.base.MyPage;
import cn.shop.thiredparty.config.OSSConfig;
import cn.shop.transcation.backSetting.entity.BackSetting;
import cn.shop.utils.ApiResult;
import cn.shop.utils.Putif;
import cn.shop.utils.constant.SystemSettingConstant;
import cn.shop.validation.Save;
import cn.shop.validation.Update;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (MainSiteAnnouncement)表控制层
 *
 * @author 痴
 * @since 2022-05-18 23:40:27
 */
@Validated
@RestController
@RequestMapping("/api/v1/mainSiteAnnouncement")
public class MainSiteAnnouncementController {

	/**
	 * 服务对象
	 */
	@Resource
	private MainSiteAnnouncementService mainSiteAnnouncementService;

	@Resource
	private BackSettingService backSettingService;

	@Resource
	private MainSiteGamesService gamesService;
	@Resource
	private OSSConfig ossConfig;

	/**
	 * 获取服务协议
	 *
	 * @return 所有数据
	 * @author 痴
	 * @since 2022-05-18 23:40:27
	 */
	@GetMapping("queryServiceAgreement")
	public ApiResult<BackSetting> queryServiceAgreement() {
		BackSetting serviceAgreement = backSettingService.getOneByKey(SystemSettingConstant.SERVICE_AGREEMENT);
		return ApiResult.success(serviceAgreement);
	}

	/**
	 * 获取服务协议
	 *
	 * @return 所有数据
	 * @author 痴
	 * @since 2022-05-18 23:40:27
	 */
	@GetMapping("queryPrivacyPolicy")
	public ApiResult<BackSetting> queryPrivacyPolicy() {
		BackSetting serviceAgreement = backSettingService.getOneByKey(SystemSettingConstant.PRIVACY_POLICY);
		return ApiResult.success(serviceAgreement);
	}

	/**
	 * 获取最新10条
	 *
	 * @return 所有数据
	 * @author 痴
	 * @since 2022-05-18 23:40:27
	 */
	@GetMapping("queryNews")
	public ApiResult<List<MainSiteAnnouncement>> queryNews() {
		MyPage<MainSiteAnnouncement> myPage = new MyPage<>();
		myPage.setCurrent(1).setSize(10).addOrder(OrderItem.desc("update_time"));
		LambdaQueryWrapper<MainSiteAnnouncement> lambdaQuery = Wrappers.lambdaQuery();
		lambdaQuery.select(MainSiteAnnouncement::getTitle, MainSiteAnnouncement::getId);
		return ApiResult.success(mainSiteAnnouncementService.page(myPage, lambdaQuery).getRecords());
	}

	/**
	 * 获取客户端游戏
	 *
	 * @return 返回结果
	 */
	@GetMapping("/clientGames")
	public ApiResult<List<Map<String, Object>>> clientGames() {
		List<MainSiteGames> list = gamesService.lambdaQuery()
				.eq(MainSiteGames::getType, 0)
				.orderByDesc(MainSiteGames::getUpdateTime)
				.list();
		return ApiResult.success(list.stream().map(this::format).collect(Collectors.toList()));
	}

	/**
	 * 获取手机端游戏
	 *
	 * @return 返回结果
	 */
	@GetMapping("/mobileGames")
	public ApiResult<List<Map<String, Object>>> mobileGames() {
		List<MainSiteGames> list = gamesService.lambdaQuery()
				.eq(MainSiteGames::getType, 1)
				.orderByDesc(MainSiteGames::getUpdateTime)
				.list();
		return ApiResult.success(list.stream().map(this::format).collect(Collectors.toList()));
	}

	/**
	 * 获取最新的5条合作游戏
	 *
	 * @return 返回结果
	 */
	@GetMapping("/latestGames")
	public ApiResult<List<Map<String, Object>>> latestvGames() {
		MyPage<MainSiteGames> page = new MyPage<>(1, 5);
		List<MainSiteGames> list = gamesService.lambdaQuery()
				.orderByDesc(MainSiteGames::getUpdateTime)
				.page(page).getRecords();
		return ApiResult.success(list.stream().map(this::format).collect(Collectors.toList()));
	}


	/**
	 * 查询所有数据
	 *
	 * @param mainSiteAnnouncement 查询实体
	 * @return 所有数据
	 * @author 痴
	 * @since 2022-05-18 23:40:27
	 */
	@GetMapping("queryAll")
	public ApiResult<List<MainSiteAnnouncement>> queryAll(MainSiteAnnouncement mainSiteAnnouncement) {
		return ApiResult.success(mainSiteAnnouncementService.list(new QueryWrapper<>(mainSiteAnnouncement)));
	}

	/**
	 * 分页查询所有数据
	 *
	 * @param page                 分页对象
	 * @param mainSiteAnnouncement 查询实体
	 * @return 所有数据
	 * @author 痴
	 * @since 2022-05-18 23:40:27
	 */
	@GetMapping("queryByPage")
	public ApiResult<MyPage<MainSiteAnnouncement>> queryByPage(MyPage<MainSiteAnnouncement> page, MainSiteAnnouncement mainSiteAnnouncement) {
		return ApiResult.success(mainSiteAnnouncementService.page(page, new QueryWrapper<>(mainSiteAnnouncement)));
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 * @author 痴
	 * @since 2022-05-18 23:40:27
	 */
	@GetMapping("queryOne/{id}")
	public ApiResult<MainSiteAnnouncement> selectOne(@PathVariable Serializable id) {
		return ApiResult.success(mainSiteAnnouncementService.getById(id));
	}

	/**
	 * 新增数据
	 *
	 * @param mainSiteAnnouncement 实体对象
	 * @return 新增结果
	 * @author 痴
	 * @since 2022-05-18 23:40:27
	 */
	@PostMapping("insert")
	public ApiResult<Map<String, Object>> insert(@Validated(Save.class) @RequestBody MainSiteAnnouncement mainSiteAnnouncement) {
		return ApiResult.result(mainSiteAnnouncementService.save(mainSiteAnnouncement));
	}

	/**
	 * 修改数据
	 *
	 * @param mainSiteAnnouncement 实体对象
	 * @return 修改结果
	 * @author 痴
	 * @since 2022-05-18 23:40:27
	 */
	@PutMapping("update")
	public ApiResult<Map<String, Object>> update(@Validated(Update.class) @RequestBody MainSiteAnnouncement mainSiteAnnouncement) {
		return ApiResult.result(mainSiteAnnouncementService.updateById(mainSiteAnnouncement));
	}

	/**
	 * 删除单条数据
	 *
	 * @param id 主键
	 * @return 删除结果
	 * @author 痴
	 * @since 2022-05-18 23:40:27
	 */
	@DeleteMapping("delete/{id}")
	public ApiResult<Map<String, Object>> delete(@PathVariable Long id) {
		MainSiteAnnouncement mainSiteAnnouncement = new MainSiteAnnouncement();
		mainSiteAnnouncement.setId(id);
		return ApiResult.result(mainSiteAnnouncementService.removeById(mainSiteAnnouncement));
	}

	/**
	 * 批量删除数据
	 *
	 * @param idList 主键结合
	 * @return 删除结果
	 * @author 痴
	 * @since 2022-05-18 23:40:27
	 */
	@DeleteMapping("batchDelete")
	public ApiResult<Map<String, Object>> batchDelete(@RequestParam("idList") List<Long> idList) {
		return ApiResult.result(mainSiteAnnouncementService.batchDelete(idList, new MainSiteAnnouncement()));
	}


	private Map<String, Object> format(MainSiteGames game) {
		return Putif.start()
				.anyway("url", game.getUrl())
				.anyway("picture", ossConfig.getPrefix() + game.getPicture())
				.anyway("title", game.getTitle())
				.get();
	}
}

